import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutterroom/pages/home/index.dart';
import 'package:flutterroom/pages/login.dart';
import 'package:flutterroom/pages/my/my_add_room.dart';
import 'package:flutterroom/pages/my/my_room_list.dart';
import 'package:flutterroom/pages/not_found.dart';
import 'package:flutterroom/pages/register.dart';
import 'package:flutterroom/pages/room_detail/index.dart';
import 'package:flutterroom/pages/room_detail/room_detail_widget.dart';

class Routes {
//定义路由名称
  static Router router;
  static String home = "/";
  static String login = "/login";
  static String register = "/register";
  static String roomDetail = "/roomDetail/:roomId";
  static String myRoom = "/myRoom";
  static String roomAdd = "/roomAdd";
  // static String roomDetail = "/roomAdd";


//定义路由处理函数

  static Handler _homeHandler =
      Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    return HomePage();
  });

//登录
  static Handler _loginHandler =
      Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    return LoginPage();
  });


//注册
  static Handler _registerHandler =
      Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    return RegisterPage();
  });
  //房屋管理
  static Handler _myRoomHandler =
      Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    return MyRoomSetList();
  });
  
//添加房源
static Handler _roomAddHandler =
      Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    return MyAddRoom();
  });


  static Handler _notFoundHandler =
      Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    print("错误在不在这啊");
    return NotFoundPage();
  });
  
//房屋详情
  static Handler _roomDetailHandler =
      Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
    return RoomDetailWidget(
      roomId: params["roomId"][0],
    );
  });

//编写函数 关联路由名称和处理函数
  static void configureRoutes(Router router) {
    router.define(home, handler: _homeHandler);
    router.define(login, handler: _loginHandler);
    router.define(roomDetail, handler: _roomDetailHandler);
    router.define(register, handler: _registerHandler);
    router.define(myRoom, handler: _myRoomHandler);
    router.define(roomAdd, handler: _roomAddHandler);
    router.notFoundHandler = _notFoundHandler;
  }

  // 对参数进行encode，解决参数中有特殊字符，影响fluro路由匹配
  static Future navigateTo(BuildContext context, String path,
      {Map<String, dynamic> params,
      TransitionType transition = TransitionType.native}) {
    String query = "";
    if (params != null) {
      int index = 0;
      for (var key in params.keys) {
        var value = Uri.encodeComponent(params[key]);
        if (index == 0) {
          query = "?";
        } else {
          query = query + "\&";
        }
        query += "$key=$value";
        index++;
      }
    }
    print('我是navigateTo传递的参数：$query');

    path = path + query;
    return router.navigateTo(context, path, transition: transition);
  }
}
